package com.ewzj.bigdata.mvp.ui.adapter

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.loadImgwithloading
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.model.rspbean.InfoBean
import com.ewzj.bigdata.mvp.model.rspbean.VideoBean
import com.ewzj.bigdata.mvp.ui.activity.*


/**
 * author:zhangjing
 * 作用:
 * return:
 */

class InfoAdapter(data: List<InfoBean>) : BaseMultiItemQuickAdapter<InfoBean, BaseViewHolder>(data) {

    init {
        //一张图 type2
        addItemType(2, R.layout.item_infoone)
        //        多张图 type3
        addItemType(3, R.layout.item_infotwo)
        //        全部文字
        addItemType(1, R.layout.item_infothree)
        //        一张大图
        addItemType(4, R.layout.item_infofour)
    }

    override fun getDefItemViewType(position: Int): Int {
        return super.getDefItemViewType(position)
    }

    override fun convert(baseViewHolder: BaseViewHolder, infoBean: InfoBean) {
        if (infoBean.type_id != 5 && infoBean.type_id != 6) {
            baseViewHolder.setText(R.id.tv_name, infoBean.title)
            baseViewHolder.setText(R.id.tv_read, infoBean.visit + "")
            baseViewHolder.setText(R.id.tv_discuss, infoBean.comment + "")
            baseViewHolder.setText(R.id.tv_source, infoBean.sources)
            baseViewHolder.setText(R.id.tv_time, infoBean.create_time)

        }

        baseViewHolder.getConvertView().setOnClickListener {
            if (infoBean.type_id == 5) {
                //                    PhotoPreview.builder()
                PhotoPreViewActivity.startPhotoViewActivity(mContext, infoBean.album)
            } else if (!TextUtils.isEmpty(infoBean.video)) {
                val videoBean = VideoBean()
                videoBean.thumb = infoBean.thumb
                videoBean.title = infoBean.title
                videoBean.video = infoBean.video
                videoBean.go_url = infoBean.go_url
                videoBean.is_self = infoBean.is_self
                videoBean.three_url = infoBean.three_url
                VideoWebActivity.startVideoWebView(mContext, videoBean)
            } else {
                if (infoBean.cont_type != 1) {
                    if (!TextUtils.isEmpty(infoBean.go_url)) {
                        WebViewActivity.startWebView(mContext, infoBean.title, infoBean.go_url)
                    }
                } else {
                    AppKey.mchaneObject=infoBean
                    NewsWebActivity.startInfoActivity(mContext, infoBean)
                }
            }
        }
        when (baseViewHolder.itemViewType) {
            type2 -> {
                if (TextUtils.isEmpty(infoBean.tags)) {
                    settag(null, baseViewHolder.getView<View>(R.id.tv_name) as TextView)
                } else {
                    settag(infoBean, baseViewHolder.getView<View>(R.id.tv_name) as TextView)
                }
                baseViewHolder.getView<ImageView>(R.id.tv_pic).loadImgwithloading(infoBean.thumb)
            }
            type3 -> {
                if (TextUtils.isEmpty(infoBean.tags)) {
                    settag(null, baseViewHolder.getView<View>(R.id.tv_name) as TextView)
                } else {
                    settag(infoBean, baseViewHolder.getView<View>(R.id.tv_name) as TextView)
                }
                if (infoBean.getPics()!=null&&infoBean.getPics().size>0){
                    baseViewHolder.getView<ImageView>(R.id.iv_pic).loadImgwithloading(infoBean.getPics().get(0))
                }
            }
            type1 -> if (TextUtils.isEmpty(infoBean.tags)) {
                settag(null, baseViewHolder.getView<View>(R.id.tv_name) as TextView)
            } else {
                settag(infoBean, baseViewHolder.getView<View>(R.id.tv_name) as TextView)
            }
            type4 -> if (!TextUtils.isEmpty(infoBean.video)) {
                baseViewHolder.getView<View>(R.id.ll_video_info).visibility = View.VISIBLE
                baseViewHolder.getView<View>(R.id.iv_tag).visibility = View.GONE
                baseViewHolder.setText(R.id.tv_video_title, infoBean.title)
                baseViewHolder.setText(R.id.tv_longtime, infoBean.duration)
                baseViewHolder.getView<ImageView>(R.id.iv_video).loadImgwithloading(infoBean.thumb)
            } else {
                baseViewHolder.getView<View>(R.id.ll_video_info).visibility = View.GONE
                if (!TextUtils.isEmpty(infoBean.thumb)) {
                    if (TextUtils.isEmpty(infoBean.tags)) {
                        setimageviewtag(null, baseViewHolder.getView<View>(R.id.iv_tag))
                    } else {
                        setimageviewtag(infoBean, baseViewHolder.getView<View>(R.id.iv_tag))
                    }
                    baseViewHolder.getView<ImageView>(R.id.iv_video).loadImgwithloading(infoBean.thumb)
                } else {
                    baseViewHolder.getView<View>(R.id.iv_video).visibility = View.GONE
                    setimageviewtag(null, baseViewHolder.getView<View>(R.id.iv_tag))
                }
            }
        }
    }


    internal fun settag(infoBean: InfoBean?, textView: TextView) {
        if (infoBean == null) {
            //            AppUtils.setTextleftDrawable(textView, 0);
            return
        }
        when (infoBean.tags) {
            "ads" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.ad_info)
            "hot" ->  Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.home_infhot)
            "vip" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.home_infzhuan)
            "new" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.icon_new)
        }//                AppUtils.setTextleftDrawable(textView, R.drawable.home_infhot);
        //                AppUtils.setTextleftDrawable(textView, R.drawable.home_infzhuan);
        //                AppUtils.setTextleftDrawable(textView, R.drawable.home_infzhuan);
    }

    internal fun setimageviewtag(infoBean: InfoBean?, view: View) {
        val imageView = view as ImageView
        if (infoBean == null) {
            imageView.visibility = View.GONE
            return
        }
        view.setVisibility(View.VISIBLE)
        when (infoBean.tags) {
            "ads" -> imageView.setImageResource(R.drawable.icon_ad)
            "hot" -> imageView.setImageResource(R.drawable.icon_hot)
            "vip" -> imageView.setImageResource(R.drawable.icon_zhuan)
            "new" -> imageView.setImageResource(R.drawable.icon_new2)
        }
    }

    companion object {
        val type1 = 1
        val type2 = 2
        val type3 = 3
        val type4 = 4
    }
}
